#!/usr/bin/perl
use warnings;
use strict;
#根据差异表达基因ID，对照基因组参考序列文件，筛选go_terms;
#@ARGV依次为差异基因文件，gene referense文件，输出结果文件(3列，gene_id,locus,go_terms)；
sub get_go_term{
    die("usage:perl $0 infile outfile") unless @ARGV==3;
	
	open IN,"$ARGV[0]" or die "can't open file $ARGV[0]";
	while(<IN>){
		@rows = split(/\s+/);
		push @data,[@rows];
	}
	close IN;		#$data[0][0]&$data[0][1];$data[$_][0];$data[$_][1];
	
	open STDOUT,">>","$ARGV[2]" or die "can't create file $ARGV[2]!";
#	push @row,$data[0][0];
	push @row,$data[0][1];
	push @row,go_terms;
	print "@row\n";
	close STDOUT;
	
	open IN,"$ARGV[1]" or die "can'open file $ARGV[1]";
	while (<IN>){
		if ($_ =~ /(?:>)(\w+\.\d)\s\w+/){
	#	print "$1\n";
			foreach(1..$#data){
				my @go;
				print "@go\n";
				if ($data[$_][1] eq $1 && $' =~ /go_terms:(?<name1>.*?)\s+/){		#$+{name1}中有go terms;
	#				print "有输出!\n";
					@term = split(/,/,$+{name1});
	#				push @go,$data[$_][0];
					push @go,$data[$_][1];
					push @go,@term;
					open STDOUT,">>","$ARGV[2]" or die "can't create file $ARGV[2]!";
					print "@go\n";
					close STDOUT;
				}#else {print "无法输出!\n";}
			}
		}
	}
	close IN;

}

&get_go_term;